home *** CD-ROM | disk | FTP | other *** search
- _PROGRAMMER'S BOOKSHELF COLUMN_
- by Andrew Schulman
-
-
- Example 1: A simple Windows program, SYSTRES.C, consists of
- little more than the OkMsgBox() function from Petzold's Programming
- Windows.
-
- /* SYSTRES.C -- System Resources
- Borland C++:
- bcc -W systres.c
- rc systres.exe */
-
- #include <windows.h>
-
- /* undocumented Windows call: KERNEL.138 */
- extern DWORD FAR PASCAL GetHeapSpaces(WORD hModule);
- void heap_info(char *module, WORD *pfree, WORD *ptotal, WORD *ppercent)
- {
- DWORD info = GetHeapSpaces(GetModuleHandle(module));
- *pfree = LOWORD(info);
- *ptotal = HIWORD(info);
- *ppercent = (WORD) ((((DWORD) *pfree) * 100L) / ((DWORD) *ptotal));
- }
-
- #define PROGMAN_EXE "progman.exe"
- #define PROGMAN_CLASS "Progman"
- #define HELP_ABOUT 0x387 // subject to change!!!
-
- // run the Program Manager Help menu About... box
- void progmgr_aboutbox(void)
- {
- WORD progmgr;
- // use class name ("Progman"), not window title ("Program Manager"):
- // see Richter, Windows 3: A Developer's Guide, p.80
- if (! (progmgr = FindWindow(PROGMAN_CLASS, 0)))
- {
- // WinExec() is async: equivalent of spawn() P_NOWAIT
- WinExec(PROGMAN_EXE, SW_SHOWMINIMIZED);
- if (! (progmgr = FindWindow(PROGMAN_CLASS, 0)))
- return; // couldn't find or load Program Manager
- }
- // use PostMessage instead of SendMessage so we run async
- PostMessage(progmgr, WM_COMMAND, HELP_ABOUT, 0L);
- }
- // from Petzold, Programming Windows, p. 441
- void OkMsgBox(char *szCaption, char *szFormat, ...)
- {
- char szBuffer[256] ;
- char *pArguments ;
- pArguments = (char *) &szFormat + sizeof szFormat ;
- wvsprintf(szBuffer, szFormat, pArguments) ; // changed from vsprintf
- MessageBox(NULL, szBuffer, szCaption, MB_OK) ;
- }
- int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine,
- int nCmdShow)
- {
- WORD user_free, user_total, user_percent;
- WORD gdi_free, gdi_total, gdi_percent;
- heap_info("USER", &user_free, &user_total, &user_percent);
- heap_info("GDI", &gdi_free, &gdi_total, &gdi_percent);
- progmgr_aboutbox();
- OkMsgBox("System Resources",
- "USER heap: %u bytes free out of %u (%u%% free)\n"
- "GDI heap: %u bytes free out of %u (%u%% free)\n"
- "Free system resources: %u%%\n",
- user_free, user_total, user_percent,
- gdi_free, gdi_total, gdi_percent,
- min(user_percent, gdi_percent));
- }
-
-
-
-
-
- Example 2: The same utility as in Example 1, but this time written with
- the Realizer BASIC development environment for Windows.
-
- ' SYSTRES.RLZ -- System Resources
- ' run-time dynamic linking to Windows API
- external "kernel" func GetHeapSpaces (word) as dword
- external "kernel" func GetModuleHandle(pointer) as word
- external "user" func FindWindow(pointer, pointer) as word
- external "user" proc PostMessage(word, word, word, dword)
-
- func get_free(modname)
- heap_space = GetHeapSpaces(GetModuleHandle(modname))
- free = heap_space mod 65536 ' LOWORD
- total = Floor(heap_space / 65536) ' HIWORD
- percent = 100 * free / total
- Print #1; modname, "heap: ", free, " bytes free out of ", total;
- Print #1; " (", percent, "%)"
- return percent
- end func
-
- WM_COMMAND = 273 ' 111h (yuk! no hex!)
- HELP_ABOUT = 903 ' 387h
-
- ' pull down Program Manager Help About... box
- progmgr = FindWindow("Progman", Pointer(0))
- if progmgr = 0 then
- Shell "progman", _Minimize
- progmgr = FindWindow("Progman", Pointer(0))
- end if
- PostMessage(progmgr, WM_COMMAND, HELP_ABOUT, 0)
-
- LogNew(1; "System Resources")
- user_percent = get_free("USER")
- gdi_percent = get_free("GDI")
- Print #1; "System Resources: ", min(user_percent, gdi_percent), "% free"
- LogControl(_Show)
-
-
-
-
- Example 3: "hello world!" using the Oriel graphical batch language
-
- { HELLO.ORL -- "hello world" for Oriel }
-
- UseCaption("Hello")
- SetMenu("&File", IGNORE, "E&xit", Do_Exit, ENDPOPUP)
- UseFont("Roman", 0, 24, BOLD, NOITALIC, NOUNDERLINE, 64, 0, 0)
- DrawText(10, 10, "Hello world!")
- WaitInput()
- Do_Exit:
- End
-
-